import random
class Game:
    #构造函数
    def __init__(self):
        self.data = [([0]*4) for i in range(4)]  # 4*4的列表
        for i in range(0,4):
            for j in range(0,4):
                self.data[i][j]=0
    #显示内容
    def display(self):
        print('--------')
        for i in range(0,4):
            for j in range(0,4):
                print(str(self.data[i][j]) + " ",end="" )
            print() #换行
        print('--------')
    #生成随机数
    def getRandom(self):
        iTotal=0  #记录0单元格总数，生成随机数决定数字放哪个单元格
        for i in range(0,4):
            for j in range(0,4):
                if(self.data[i][j]==0):
                    iTotal=iTotal+1
        iIndex=random.randint(1,iTotal)
        iTotal=0
        for x in range(0,4):
            for y in range(0,4):
                if (self.data[x][y] == 0):
                    iTotal = iTotal+1
                    if( iTotal==iIndex):
                        self.data[x][y]=int(random.choice('24'))    #随机生成24
                        return
    #左移动
    def leftMove(self):
        for i in range(4):
            for j in range(3):
                for k in range(j+1, 4):
                    if self.data[i][k] > 0:
                        if self.data[i][j] == 0:
                            self.data[i][j] = self.data[i][k]
                            self.data[i][k] = 0
                        elif self.data[i][j] == self.data[i][k]:
                            self.data[i][j] *= 2
                            self.data[i][k] = 0
                        break

    #向下移动
    def downMove(self):
        for i in range(4):
            for j in range(3, 0, -1):
                for k in range(j - 1, -1, -1):
                    if self.data[k][i] > 0:
                        if self.data[j][i] == 0:
                            self.data[j][i] = self.data[k][i]
                            self.data[k][i] = 0
                        elif self.data[j][i] == self.data[k][i]:
                            self.data[j][i] *= 2
                            self.data[k][i] = 0
                        break
    #向上移动
    def upMove(self):
        for i in range(4):
            for j in range(3):
                for k in range(j + 1, 4):
                    if self.data[k][i] > 0:
                        if self.data[j][i] == 0:
                            self.data[j][i] = self.data[k][i]
                            self.data[k][i] = 0
                        elif self.data[k][i] == self.data[j][i]:
                            self.data[j][i] *= 2
                            self.data[k][i] = 0
                        break
    #向右移动
    def rightMove(self):
        for i in range(4):
            for j in range(3, 0, -1):
                for k in range(j - 1, -1, -1):
                    if self.data[i][k] > 0:
                        if self.data[i][j] == 0:
                            self.data[i][j] = self.data[i][k]
                            self.data[i][k] = 0
                        elif self.data[i][j] == self.data[i][k]:
                            self.data[i][j] *= 2
                            self.data[i][k] = 0
                        break
def main():
    print("a向左，d向右,w向上，x向下，q退出")
    game = Game()
    game.getRandom()
    game.display()
    while(1):
        a=input()
        if(a=="a"):
            game.leftMove()
            game.getRandom()
            game.display()
        elif(a=="x"):
            game.downMove()
            game.getRandom()
            game.display()
        elif(a=="w"):
            game.upMove()
            game.getRandom()
            game.display()
        elif (a == "d"):
            game.rightMove()
            game.getRandom()
            game.display()
        elif(a=="q"):
            break

if __name__ == '__main__':
    main()
